home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / DesktopDoubler / Common / Sources / PatchHarness.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-23  |  2.8 KB  |  153 lines  |  [TEXT/CWIE]

  1. #define DISABLE_LOCAL_CALLTRACE        1        // Set to 1 to disable Call Traces for this file.
  2. #define DISABLE_LOCAL_DEBUG            0        // Set to 1 to disable all debugging for this file.
  3. #include "DebugUtils.h"
  4.  
  5. #include <Errors.h>
  6. #include <Gestalt.h>
  7. #include <Memory.h>
  8. #include <Resources.h>
  9. #include "PatchHarness.h"
  10.  
  11.  
  12.  
  13.  
  14.  
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18.  
  19. static Boolean OnPowerPC(void);
  20.  
  21. #ifdef __cplusplus
  22. }
  23. #endif
  24.  
  25.  
  26.  
  27.  
  28.  
  29. // Load patch harnesses for all the system functions that
  30. // we need to hook.  These harnesses will allow us to unload
  31. // and reload ourselves without rebooting by giving us the
  32. // ability to change the patch chain whenever we want.
  33. OSStatus InstallPatchHarness(PatchDesc **patchList)
  34. {
  35.     Handle                code;
  36.     short                index;
  37.     OSStatus            err;
  38.     
  39.     
  40.     // Keep things somewhat generic, just index thru and load all
  41.     // the 'PTCH' code resources, they contain all the 68K harnesses.
  42.     index = 1;
  43.     while((code = Get1IndResource('PTCH',index++)) != NULL)
  44.     {
  45.         // Make sure the code resource will
  46.         // stick around after we are gone.
  47.         DetachResource(code);
  48.         HLock(code);
  49.         
  50.         // Install the patch harness.
  51.         err = CallPatchInitProc(*code,patchList);
  52.         if (err != noErr)
  53.         {
  54.             DisposeHandle(code);
  55.             return err;
  56.         }
  57.     }
  58.     
  59.     if (OnPowerPC())
  60.     {
  61.         // Keep things somewhat generic, just index thru and load all
  62.         // the 'ptch' code resources, they contain all the PPC harnesses.
  63.         index = 1;
  64.         while((code = Get1IndResource('ptch',index++)) != NULL)
  65.         {
  66.             // Make sure the code resource will
  67.             // stick around after we are gone.
  68.             DetachResource(code);
  69.             HLock(code);
  70.             
  71.             // Install the patch harness.
  72.             err = CallPatchInitProc(*code,patchList);
  73.             if (err != noErr)
  74.             {
  75.                 DisposeHandle(code);
  76.                 return err;
  77.             }
  78.         }
  79.     }
  80.     
  81.     return noErr;
  82. }
  83.  
  84.  
  85.  
  86.  
  87.  
  88. OSStatus InstallPatch(PatchDesc *patchList,OSType type,UniversalProcPtr patch)
  89. {
  90.     PatchDesc    *desc;
  91.     
  92.     
  93.     // Find patch descriptor that matches type.
  94.     for (desc = patchList;desc != NULL;desc = desc->next)
  95.         if (desc->type == type)
  96.         {
  97.             // Check that its OK to install.
  98.             if (desc->install == NULL)
  99.                 return -1;
  100.             
  101.             // Install the patch.
  102.             CallPatchInstallProc(desc->install,desc,patch);
  103.             return noErr;
  104.         }
  105.     
  106.     // Unknown type.
  107.     return -1;
  108. }
  109.  
  110.  
  111.  
  112.  
  113.  
  114. OSStatus RemovePatch(PatchDesc *patchList,OSType type)
  115. {
  116.     PatchDesc    *desc;
  117.     
  118.     
  119.     // Find patch descriptor that matches type.
  120.     for (desc = patchList;desc != NULL;desc = desc->next)
  121.         if (desc->type == type)
  122.         {
  123.             // Check that its OK to remove.
  124.             if (desc->remove == NULL)
  125.                 return -1;
  126.             
  127.             // Remove the current patch.
  128.             CallPatchRemoveProc(desc->remove,desc);
  129.             return noErr;
  130.         }
  131.     
  132.     // Unknown type.
  133.     return -1;
  134. }
  135.  
  136.  
  137.  
  138.  
  139.  
  140. Boolean OnPowerPC(void)
  141. {
  142.     long        result;
  143.     OSStatus    err;
  144.     
  145.     
  146.     result = gestalt68k;
  147.     err = Gestalt(gestaltSysArchitecture,&result);
  148.     if ((err == noErr) && (result == gestaltPowerPC))
  149.         return true;
  150.     
  151.     return false;
  152. }
  153.